//给你二叉树的根节点 root 和一个整数目标和 targetSum ，找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。 
//
// 叶子节点 是指没有子节点的节点。 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
//输出：[[5,4,11,2],[5,8,4,5]]
// 
//
// 示例 2： 
//
// 
//
// 
//输入：root = [1,2,3], targetSum = 5
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：root = [1,2], targetSum = 0
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点总数在范围 [0, 5000] 内 
// -1000 <= Node.val <= 1000 
// -1000 <= targetSum <= 1000 
// 
//
// 注意：本题与主站 113 题相同：https://leetcode-cn.com/problems/path-sum-ii/ 
//
// Related Topics 树 深度优先搜索 回溯 二叉树 👍 437 👎 0


package leetcode.editor.cn;

import utils.CreateNode;
import utils.TreeNode;

import java.util.*;

class 二叉树中和为某一值的路径{
    public static void main(String[] args) {
        Solution solution = new 二叉树中和为某一值的路径().new Solution();
        CreateNode createNode = new CreateNode();
        Queue<Integer> tree = new LinkedList<>(Arrays.asList(5,4,11,7,null,null,2,null,null,null,8,13,null,null,4,5,null,null,1,null,null));
        TreeNode root = new TreeNode(tree.remove());
        createNode.createTree(tree,root);
        System.out.println(root.printTree());
        System.out.println(solution.pathSum(root,22));
/*        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(11);
        TreeNode node4 = new TreeNode(7);
        TreeNode node5 = new TreeNode(2);
        TreeNode node6 = new TreeNode(8);
        TreeNode node7 = new TreeNode(13);
        TreeNode node8 = new TreeNode(4);
        TreeNode node9 = new TreeNode(5);
        TreeNode node10 = new TreeNode(1);
        node1.left = node2;
        node2.left = node3;
        node3.left = node4;
        node3.right = node5;
        node1.right = node6;
        node6.left = node7;
        node6.right = node8;
        node8.left = node9;
        node8.right = node10;
        
        System.out.println(solution.pathSum(node1,22));*/
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    List<List<Integer>> path = new LinkedList<List<Integer>>();
    Deque<Integer> list = new LinkedList<Integer>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        if (root == null) return new ArrayList<>();
        dfs(root,target,0);
        return path;
    }
    
    public void dfs(TreeNode node, int target, int sum){
        list.addLast(node.val);
        sum += node.val;
        
        if (node.left == null && node.right == null && target == sum ){
            path.add(new LinkedList<>(list));
            list.removeLast();
            return;
        }
        if (node.left != null){
            dfs(node.left,target,sum);
        }
        if (node.right != null){
            dfs(node.right,target,sum);
        }
        list.removeLast();
    }
    
    
}
//leetcode submit region end(Prohibit modification and deletion)

}

